import com.lex.Main;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

import java.math.BigDecimal;
import java.text.NumberFormat;

@SpringBootTest
@ContextConfiguration(classes = {Main.class})
public class test {
    @Test
    public void test() {
        String str = "123123123.445555444545,33.2254";
        //取出字符串逗号前边的字符串
        String substring = str.substring(0, str.indexOf(","));
        //取出字符串逗号后边的字符串
        String substring1 = str.substring(str.indexOf(",")+1);
//        System.out.println(substring);
//        System.out.println(substring1);


//        double v = Double.parseDouble(substring);
//        double v1 = Double.parseDouble(substring1);
//        System.out.println(v);
//        System.out.println(v1);
//        NumberFormat nf = NumberFormat.getNumberInstance();
//        nf.setMaximumFractionDigits(2);
//        //格式化小数
//        String format = nf.format(v);
//        String format1 = nf.format(v1);
//        System.out.println(format);
//        System.out.println(format1);
//
//        //转成BigDecimal
//        BigDecimal bigDecimal = new BigDecimal(v);
//        BigDecimal bigDecimal1 = new BigDecimal(v1);
//        System.out.println(bigDecimal);
//        System.out.println(bigDecimal1);
//        //编写一个经纬度计算距离的方法
//        double distance = getDistance(116.404, 39.915, 121.491, 31.233);
//        System.out.println(distance); //km

            //substring转换成double，保留六位位小数
            double v = Double.parseDouble(String.format("%.6f", Double.parseDouble(substring)));
            double v1 = Double.parseDouble(String.format("%.6f", Double.parseDouble(substring1)));
            System.out.println(v);
            System.out.println(v1);
    }

    @Test
    public void test2(){
        String lang1 = "113.30938720703125";
        String lat1 = "40.13222";
        String lang2 = "113.30735668945312";
        String lat2 = "40.135256";
        Double lang1_format = Double.parseDouble(String.format("%.6f", Double.parseDouble(lang1)));
        Double lat1_format = Double.parseDouble(String.format("%.6f", Double.parseDouble(lat1)));
        Double lang2_format = Double.parseDouble(String.format("%.6f", Double.parseDouble(lang2)));
        Double lat2_format = Double.parseDouble(String.format("%.6f", Double.parseDouble(lat2)));
        System.out.println(lang1_format);
        System.out.println(lat1_format);
        System.out.println(lang2_format);
        System.out.println(lat2_format);
//        double distance = getDistance(lang1_format, lat1_format, lang2_format, lat2_format);
        double[] param = {lang1_format, lat1_format, lang2_format, lat2_format};
        double distance = get_d(param);
        System.out.println("两地相距：" + distance + "m");
    }
    public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
        //这部分代码精确到米
        double EARTH_RADIUS = 6378137; // 地球平均赤道半径，单位：米
        double radLat1 = lat1 * Math.PI / 180.0;
        double radLat2 = lat2 * Math.PI / 180.0;
        double a = radLat1 - radLat2;
        double b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;

        double s = 2 * Math.asin(Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) +
                        Math.cos(radLat1) * Math.cos(radLat2) *
                                Math.pow(Math.sin(b / 2), 2)
        ));
        s = s * EARTH_RADIUS;
        s = Math.round(s); // 直接使用整数舍入，保留到米
        return s;
    }
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double get_d(double[] param){
        double qdjd = param[0]; //第一个经度
        double qdwd = param[1]; //第一个维度
        double qtjd = param[2]; //第二个经度
        double qtwd = param[3]; //第二个维度
        double EARTH_RADIUS = 6378137;
        double radLat1 = qdwd * Math.PI / 180.0;
        double radLat2 = qtwd * Math.PI / 180.0;
        double a = radLat1 - radLat2;
        double b = qdjd * Math.PI / 180.0 - qtjd * Math.PI / 180.0;
        double s = 2 * Math.asin(Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) +
                        Math.cos(radLat1) * Math.cos(radLat2) *
                                Math.pow(Math.sin(b / 2), 2)
        ));
        s = s * EARTH_RADIUS;
        double  JuLi = Math.round(s);
        return JuLi;
    }
}

